<?php


namespace app\lunwen\controller;


use app\lunwen\model\DepartmentModel;
use app\lunwen\model\EducationalModel;
use app\lunwen\model\StudentModel;
use app\lunwen\model\TeacherModel;
use cmf\controller\HomeBaseController;

class CheckLoginController extends HomeBaseController
{
    public function index(){


        //echo $_POST['addtest'];
        //学生登入检测
        $student = new StudentModel();
        $data = $student->where("user_id", $_POST["userId"])
            ->where("name", $_POST["name"])
            ->where("stu_id", $_POST["studentId"])->find();
        if ($data == null) {
            echo "<script>alert('账号或密码错误')</script>";
            $this->assign("msg","ss");
            return $this->fetch(':login');
        } else {


            session('stu_name', $data->stu_name);
            session('stu_id', $_POST["studentId"]);
            $this->assign("stu_name", $data->stu_name);
            $this->assign("stu_id", $data->stu_id);

            return $this->fetch(':stuHome');
        }
    }

    //老师登入检测
    public function teacherLogin(){

        $year = $_POST["yearConfig"];
        $beforeYear = substr($year,0,7);
        $afterYear = substr($year,10,16);

        $teacher = new TeacherModel();
        $data = $teacher->where('user_id',$_POST["userId"])
            ->where('teacher_name',$_POST["teacherName"])
            ->where('teacher_id',$_POST["teacherId"])->find();
        if ($data == null) {
            return json([
                'code'=>-1,
                'msg'=>'账号或姓名或身份证错误',
            ]);
            /*return $this->fetch(':login');*/
        } else {
            session('teacher_name', $data->teacher_name);
            session('teacher_id', $_POST["teacherId"]);
            session("year", $_POST["yearConfig"]);
            session("beforeYear", $beforeYear);
            session("afterYear", $afterYear);
            /*$this->assign('teacher_name', $data->teacher_name);
            $this->assign('teacher_id', $_POST["teacherId"]);

            return $this->fetch(':teacherHome');*/
            return json([
                'msg'=>'登录成功',
                'code'=>1,
            ]);
        }

    }

    //院系检测
    public function departmentLogin(){

        $year = $_POST["yearConfig"];
        $beforeYear = substr($year,0,7);
        $afterYear = substr($year,10,16);
        $department =new DepartmentModel();
        $data = $department->where('department_name',$_POST["departmentName"])
            ->where('user_id',$_POST["userId"])
            ->where('department_id',$_POST["departmentId"])
            ->find();
        if ($data == null) {
            return json([
                'code'=>-1,
                'msg'=>'账号或姓名或身份证错误',
            ]);
        } else {
            session('department_name', $data->department_name);
            session('department_id', $_POST["departmentId"]);
            session("year", $_POST["yearConfig"]);
            session("beforeYear", $beforeYear);
            session("afterYear", $afterYear);
            $this->assign('department_name', $data->department_name);
            $this->assign('department_id', $_POST["departmentId"]);
            return json([
                'msg'=>'登录成功',
                'code'=>1,
            ]);
        }
    }

    //教秘检测
    public function educationalLogin(){
        $year = $_POST["yearConfig"];
        $beforeYear = substr($year,0,7);
        $afterYear = substr($year,10,16);

        $educational =new EducationalModel();
        $data = $educational->where('educational_name',$_POST["educationalName"])
            ->where('user_id',$_POST["userId"])
            ->where('educational_id',$_POST["educationalId"])->find();
        if ($data == null) {
            return json([
                'code'=>-1,
                'msg'=>'账号或姓名或身份证错误',
            ]);
        } else {
            session('educational_name', $data->educational_name);
            session('educational_id', $_POST["educationalId"]);
            session("year", $_POST["yearConfig"]);
            session("beforeYear", $beforeYear);
            session("afterYear", $afterYear);
            $this->assign('educational_name', $data->educational_name);
            $this->assign('educational_id', $_POST["educationalId"]);


            return json([
                'msg'=>'登录成功',
                'code'=>1,
            ]);
        }
    }
}